<?php
class Marketing_AdminEmailController extends Zendvn_Controller_Action{

	protected $_arrParam; //Mang tham so nhan duoc khi mot Action chay
	protected $_currentController; //Duong dan cua Controller
	protected $_actionMain; //Duong dan cua Action chinh
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 10,
									'currentPage' => 1
									);
	protected $_namespace;
	protected $_page = ''; //Url page
	
	public function init(){
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
		$this->_controllerMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller']; //Duong dan cua Controller chinh
	
		//Luu cac du lieu filter vaof SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//Lay thong tin so phan tu tren mot trang
		if(isset($this->_arrParam['limitPage'])){
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif(!empty($ssFilter->limitPage)){
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//Thanh vien dang login
		$info 			= new Zendvn_System_Info();
		$this->_arrParam['adminInfo'] 	= $info->getMemberInfo();
		
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $this->view->siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'daotao');
	}

	public function indexAction(){
		$this->view->headMeta()->setHttpEquiv('Refresh','3600000');
		
		$this->view->Title = 'Marketing :: Gửi mail';
		$this->view->headTitle($this->view->Title, true);
		$siteConfig = Zend_Registry::get('siteConfig');
		
		if(!empty($this->_arrParam['id'])) {
			$tblMarketing = new Marketing_Model_Item();
			$this->view->Marketing = $tblMarketing->getItem($this->_arrParam, array('task' => 'admin-info'));
			$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/xml/' . $this->_arrParam['id'] . '.xml';
			if(file_exists($filename)) {
				$xml = simplexml_load_file($filename);
				function simplexml_to_array($xmlobj) {
					$a = array();
					foreach ($xmlobj->children() as $node) {
						if (is_array($node))
							$a[$node->getName()] = simplexml_to_array($node);
						else
							$a[$node->getName()] = (string) $node;
					}
					return $a;
				}
					
				foreach($xml->children() AS $child)
				{
					$arrXml[] = simplexml_to_array($child);
				}
					
				foreach ($arrXml AS $val) {
					$item[$val['id']] = $val['id'];
					$khachhang[$val['id']] = $val;
				}
			} else {
				$item = array();
			}
			$this->view->Khachhang 	= $khachhang;
		} else {
			$this->view->Khachhang 	= array();
		}
	}
	
	
	public function editAction(){
		$this->view->Title = 'Marketing :: Sửa';
		$this->view->headTitle($this->view->Title, true);
		
		$tblItem 		= new Marketing_Model_Item();
		$tblCategory	= new Marketing_Model_Category();
		$tblUser		= new Default_Model_Users();
		$tblKhoahoc		= new Daotao_Model_Khoahoc();
		
		$this->view->Khoahoc	= $tblKhoahoc->itemInSelectbox(null, array('task' => 'get-khoahoc'));
		$this->view->Users		= $tblUser->listItem(null,array('task' => 'user-all'));
		$this->view->Category	= $tblCategory->itemInSelectbox();
		
		$this->view->Item		= $tblItem->getItem($this->_arrParam, array('task' => 'admin-edit'));
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/xml/' . $this->_arrParam['id'] . '.xml';
		if(file_exists($filename)) {
			$xml = simplexml_load_file($filename);
			function simplexml_to_array($xmlobj) {
				$a = array();
				foreach ($xmlobj->children() as $node) {
					if (is_array($node))
						$a[$node->getName()] = simplexml_to_array($node);
					else
						$a[$node->getName()] = (string) $node;
				}
				return $a;
			}
			
			foreach($xml->children() AS $child)
			{
				$arrXml[] = simplexml_to_array($child);
			}
			
			foreach ($arrXml AS $val) {
				$item[$val['id']] = $val['id'];
				$khachhang[$val['id']] = $val;
			}
		} else {
			$item = array();
		}
		$this->view->Khachhang 		= $khachhang;
		$this->view->Item['cid']	= $item;
		
		if($this->_request->isPost()){
			$validator = new Marketing_Form_ValidateItem($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$arrParam 	= $validator->getData();
				$lastID 	= $tblItem->saveItem($arrParam,array('task'=>'admin-edit'));
				
				$id_xoa = array_diff($item, $this->_arrParam['id_chon']);
				if(!empty($id_xoa)) {
					$this->xoaXmlAction($lastID, $id_xoa);
				}
				
				if(!empty($this->_arrParam['cid'])) {
					$this->view->User = $tblItem->listItem($this->_arrParam, array('task' => 'admin-user'));
					$this->appendXmlAction($lastID, $this->view->User);
				}
				
				if(empty($this->_arrParam['type'])){
					$this->_redirect($this->_actionMain . '/save/ok');
				}else{
					if($this->_arrParam['type'] == 'apply'){
						$this->_redirect($this->_controllerMain . '/edit/id/' . $lastID);
					}
					if($this->_arrParam['type'] == 'new'){
						$this->_redirect($this->_controllerMain . '/add');
					}
				}
			}
		}
	}
}



